package heima3_2.cn.osc.base;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import java.util.ArrayList;
import java.util.List;

import heima3_2.cn.osc.model.bean.Base;
import heima3_2.cn.osc.presenter.BasePresenter;
import heima3_2.cn.osc.ui.activity.DetailActivity;
import heima3_2.cn.osc.ui.view.AutoLoadListView;
import retrofit2.Response;

/**
 * Created by yaozhanlie on 2017/1/18.
 */

public abstract class OurTabBasePresenter<T, V extends Base> extends BasePresenter<T> {

    //TODO:基类要有的成员变量
    protected int mFlag;
    private OurTabBaseFragment mOurTabBaseFragment;
    private AutoLoadListView mListView;
    List<V> newBeans = new ArrayList<>();
    
    public OurTabBasePresenter(OurTabBaseFragment ourTabBaseFragment) {
        super();
        mOurTabBaseFragment = ourTabBaseFragment;
    }

    @Override
    protected void getFailureData(Throwable t) {

    }

    @Override
    protected void getSuccessData(Response<T> response) {
        List<V> dataBeans = getDataBean(response);

        //TODO:空集合，listView，Adapter,mFlag,访问网络之后拿到的been（5个元素）
        {
            if (mFlag == 0){
                newBeans.removeAll(newBeans);
                newBeans.addAll(dataBeans);
                if (newBeans.size() < 20) {
                    mOurTabBaseFragment.mSoftList.setHasMoreItems(false);
                }
            } else {
                newBeans.addAll(dataBeans);
                if (newBeans.size() < 20 * (mFlag+1)) {
                    mOurTabBaseFragment.mSoftList.setHasMoreItems(false);
                }
            }
            mOurTabBaseFragment.mSoftAdapter.setDatas(newBeans);

            mOurTabBaseFragment.mSoftList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                    Log.d("bbbbb",i+"");
                    Intent intent = new Intent(mOurTabBaseFragment.getActivity(), DetailActivity.class);
                    intent.putExtra("been", newBeans.get(i));
                    mOurTabBaseFragment.getActivity().startActivity(intent);
                }
            });
        }




        /**获取数据并且设置点击事件，然后跳转*/

    }


    @Override
    public void getData(int page) {
        mFlag = page;
//        Call<T> collectInfo = mRetrofitInterface.getCollectInfo();
//        collectInfo.enqueue(mCallback);
        doRetrofitQuest();
    }

    /**请求retrofit获取数据*/
    protected abstract void doRetrofitQuest();

    public abstract List<V> getDataBean(Response<T> response);
}
